package com.fjh.security.easy.starter.util;

import com.fjh.security.easy.starter.userdetails.CustomUser;
import org.checkerframework.checker.units.qual.C;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 获取用户信息
 *
 * @author fanjh37
 * @since 2023/1/31 19:39
 */
public class HttpSessionUtil {
    public static CustomUser getUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication.getPrincipal() instanceof CustomUser) {
            CustomUser customUser = (CustomUser) authentication.getPrincipal();
            return customUser;
        }
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        CustomUser customUser = new CustomUser();
        customUser.setUsername(userDetails.getUsername());
        return customUser;
    }
}
